import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class MainAstraweb {
	public static final String STARTPAGE = "http://jokes.astraweb.com/display.cgi?r=209156623535&c=adult.1";
	public static final String DOMAIN = "http://jokes.astraweb.com";
	public static String toURL(String url){
		if(url.toLowerCase().startsWith("http://")){
			return url;
		}else{
			return DOMAIN + url	;
		}
	}
	public static String saveTo = null;
	static {
		saveTo = "E:\\Android\\SuperJokes\\autodownload\\astraweb\\"
				+ new Date().getTime() + "\\";
		File f = new File(saveTo);
		if (!f.exists()) {
			f.mkdirs();
		}
	}
	public static void main(String[] args) throws IOException {
		Document doc = Jsoup.parse(new URL(STARTPAGE), 1000 * 30);
		String filePath = saveTo + File.separator + new Date().getTime();
		FileWriter fw = new FileWriter(new File(filePath));
		int max = 500;
		int counter=0;
		while (true) {
			if(counter>=max){
				break;
			}
			Elements nextPageLinks = doc.select("a");
			String nextPageUrl = toURL(findNextPageA(nextPageLinks));
//			<td bgcolor="#EEEEFF">
//			<font face="Verdana" size="2">
			Element contentElement = doc.select("td[bgcolor=#EEEEFF] font[face=Verdana]").first();
			String content = contentElement.text();
			System.out.println("next page:"+nextPageUrl);
			System.out.println("content:" + content);
			URL nextPageURL = new URL(nextPageUrl);
			doc = Jsoup.parse(nextPageURL, 1000 * 30);
			fw.write("|||>o<|||");
			fw.write(content);
			fw.write("\r\n");
			counter++;
		}
		fw.close();
	}
	private static String findNextPageA(Elements nextPageLinks) {
		for(int i=0;i<nextPageLinks.size();i++){
			Element each = nextPageLinks.get(i);
			String text= each.text();
			if(text==null){
				continue;
			}
			if(text.startsWith("Next Joke")){
				return toURL(each.attr("href"));
			}
		}
		return null;
	}

}
